/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Node;
import pt.utl.ist.isr.livetrack.server.LTServer;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class ZonesNodeChildFactory extends ChildFactory<String> {

    private static Map<LTServer, ZonesNodeChildFactory> instances = new HashMap<LTServer, ZonesNodeChildFactory>();

    public static ZonesNodeChildFactory getFactory(LTServer server) {
        if (!instances.containsKey(server)) {
            instances.put(server, new ZonesNodeChildFactory(server));
        }
        return instances.get(server);
    }
    private LTServer server;

    private ZonesNodeChildFactory(LTServer server) {
        this.server = server;
    }

    @Override
    protected Node createNodeForKey(String key) {
        return new ZoneNode(server, server.getPerimeter().getZones().get(key));
    }

    @Override
    protected boolean createKeys(List<String> toPopulate) {
        toPopulate.addAll(server.getPerimeter().getZones().keySet());
        Collections.sort(toPopulate, String.CASE_INSENSITIVE_ORDER);
        return true;
    }

    public void refresh() {
        super.refresh(false);
    }
}
